﻿using System.Data.Common;
using System.Data.SqlClient;
using System.Text;

namespace SimpleORM
{
    public class SqlFactorySqlServer : SqlFactory
    {
        protected override DbParameter OnCreateParameter()
        {
            return new SqlParameter();
        }

        protected override DParameter OnCreateParameter(DbParameter param)
        {
            return new DbCSQLServerParameter(OnCreateParameter());
        }

        public override IScriptFactory Paging(IScriptFactory exp, IScriptFactory orderKey, eOrder order, int pageIndex, int pageSize)
        {
            if (exp == null)
                return this;

            string orderby = order == eOrder.Ascending
                                 ? string.Format("{0} {1} {2}", stringORDERBY, orderKey.CommandText, stringASC)
                                 : string.Format("{0} {1} {2}", stringORDERBY, orderKey.CommandText, stringDEC);
            int min = pageIndex * pageSize;
            int max = (pageIndex + 1) * pageSize;


            IScriptFactory f = exp.Clone() as IScriptFactory;
            f.CommandText = new StringBuilder(string.Format(
                "SELECT __p__.* FROM ( SELECT ROW_NUMBER() OVER ( {0} ) AS __rn__, * FROM {1} ) AS __p__ WHERE __p__.__rn__ > {2} and __p__.__rn__ <= {3}", orderby, exp.CommandText, min, max));

            return f;
        }

        public override IScriptFactory Equal(IScriptFactory exp)
        {
            if (exp == null)
                return this;

            //if (exp.Parameters.Count > 0 && exp.Parameters[0].Value is Geometry)
            //{
            //    IScriptFactory f = Clone() as IScriptFactory;
            //    f.CommandText.Append(string.Concat(".STEquals(", exp.CommandText, ") = 1"));
            //    return f;
            //}

            return base.Equal(exp);
        }

        public override IScriptFactory Contains(IScriptFactory exp)
        {
            if (exp == null)
                return this;

            //if (exp.Parameters.Count > 0 && exp.Parameters[0].Value is Geometry)
            //{
            //    IScriptFactory f = Clone() as IScriptFactory;
            //    f.CommandText.Append(string.Concat(".STContains(", exp.CommandText, ") = 1"));
            //    return f;
            //}

            return base.Contains(exp);
        }

        public override IScriptFactory Intersect(IScriptFactory exp)
        {
            if (exp == null)
                return this;

            //if (exp.Parameters.Count > 0 && exp.Parameters[0].Value is Geometry)
            //{
            //    IScriptFactory f = Clone() as IScriptFactory;
            //    f.CommandText.Append(string.Concat(".STIntersects(", exp.CommandText, ") = 1"));
            //    return f;
            //}

            return base.Contains(exp);
        }

        public override IScriptFactory Within(IScriptFactory exp)
        {
            if (exp == null)
                return this;

            //if (exp.Parameters.Count > 0 && exp.Parameters[0].Value is Geometry)
            //{
            //    IScriptFactory f = Clone() as IScriptFactory;
            //    f.CommandText.Append(string.Concat(".STWithin(", exp.CommandText, ") = 1"));
            //    return f;
            //}

            return base.Contains(exp);
        }
    }
}
